package com.hq.mes.bussiness;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;
import lombok.Setter;
import me.zhengjie.utils.QueryHelp;

@Getter
@Setter
public abstract class BussinessBaseQueryCriteria {
	protected String tableName;
	protected String distinctColumnName;

	public Object[] getFirstNotNullProperty() {
		try {
			List<Field> fields = QueryHelp.getAllFields(this.getClass(), new ArrayList<>());
			for (Field field : fields) {
				field.setAccessible(true);

				String fieldName = field.getName();
//						Class<?> fieldType = field.getType();
				Object val = field.get(this);

				if (ObjectUtil.isNull(fieldName) || "".equals(fieldName) || ObjectUtil.isNull(val) || "".equals(val)) {
					continue;
				} else {
					Object[] re = { fieldName, val };
					return re;
				}

			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public Map<String, String> getAllStringPropertiesNotEmpty() {
		try {
			List<Field> fields = QueryHelp.getAllFields(this.getClass(), new ArrayList<>());
			HashMap<String, String> re = new HashMap<String, String>();
			for (Field field : fields) {
				Class<?> fieldType = field.getType();
				if (fieldType.equals(String.class)) {
					field.setAccessible(true);

					String fieldName = field.getName();

					Object val = field.get(this);

					if (ObjectUtil.isNull(fieldName) || "".equals(fieldName) || ObjectUtil.isNull(val)
							|| "".equals(val)) {
						continue;
					} else {
						re.put(fieldName, (String) val);
					}

				}

			}
			return re;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
